//
//  SendTalkBrowseView.m
//  DongDongCircle
//
//  Created by 谢曦 on 2016/12/20.
//  Copyright © 2016年 gl. All rights reserved.
//

#import "PhotoBrowseView.h"
#import "ImageBrowserManager.h"
#import "JFImagePickerController.h"
//#import "MWPhotoBrowser.h"
#define kPhotoBrowseViewW 91
@interface PhotoBrowseView ()
{
    NSArray *_dataSource;
}
@end
@implementation PhotoBrowseView

-(void)awakeFromNib{
    [super awakeFromNib];
    [self registerCellWithClass:[PhotoBrowseViewCell class] reuseIdetifier:@"PhotoBrowseViewCell"];
    self.netImages=@[].mutableCopy;
     self.photos=@[].mutableCopy;
   
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        [self registerCellWithClass:[PhotoBrowseViewCell class] reuseIdetifier:@"PhotoBrowseViewCell"];
        self.netImages=@[].mutableCopy;
         self.photos=@[].mutableCopy;
       
    }
    return self;
}
#pragma mark -UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
{
    //多了一张添加cell
  
        return _dataSource.count==5?_dataSource.count: _dataSource.count+1;
    

    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kPhotoBrowseViewW, kPhotoBrowseViewW);

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
   
    PhotoBrowseViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoBrowseViewCell" forIndexPath:indexPath];
 
        //only加载一次
    if (_isAdd==NO) {
        cell.downImageBlock=^(UIImage *image){
            [_netImages addObject:image];
            [_photos addObject:image];
            ASSETHELPER.currentPhotosCount=_netImages.count;
        };

    }
    
    
 
    if (indexPath.item==_dataSource.count) {
        //最后一个
        cell.image=[UIImage imageNamed:@"img_adphoto"];
        cell.isHiddenDeleteButton=YES;
    }
    else{
        if(_isAdd==NO){
            cell.imageUrl=_photoUrls[indexPath.row];
        }
        else  cell.image=_photos[indexPath.row];
             cell.isHiddenDeleteButton=NO;
    }
    
    
    
  
    cell.DeleteButtonClickBlock=^{
         _isAdd=YES;
        if ([self.delegate respondsToSelector:@selector(photoBrowseViewDelete:cell:)]) {
            [self.delegate photoBrowseViewDelete:indexPath.row cell:cell];
        }

          NSMutableArray *photos=_photos;
        [photos removeObjectAtIndex:indexPath.row];
       
        if (_photos.count==0) {
           _isAdd=NO;
            _dataSource=photos;
            [self.collectionView reloadData];
        }
          else self.photos=photos;
      
      
           };
    
    
    return cell;
    
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    if (indexPath.row==_dataSource.count) {
        self.isAdd=YES;
        [self addPhoto];
        return;
    }
   
    [ImageBrowserManager viewController:_currentController pushToBrowserWithPhotos:self.photos currentIndex:indexPath.row];
}


-(void)addPhoto{
    if ([self.delegate respondsToSelector:@selector(photoBrowseViewAddPhoto)]) {
        [self.delegate photoBrowseViewAddPhoto];
    }
}
-(void)setPhotos:(NSMutableArray *)photos
{
    _photos=photos;
 
    if (photos.count>0) {
         _dataSource=photos;
         [self.collectionView reloadData];
    }
   
}
-(void)setPhotoUrls:(NSArray<NSString *> *)photoUrls{
    _photoUrls=photoUrls;
     self.isAdd=NO;
    [self.collectionView reloadData];

}
-(void)setIsAdd:(BOOL)isAdd{
    _isAdd=isAdd;
    _dataSource= isAdd==YES?_photos.copy:_photoUrls.copy;
}
@end
